package com.qianfeng.security.service;

import com.qianfeng.exception.FindDataException;
import com.qianfeng.mapper.UserMapper;
import com.qianfeng.pojo.BaseUser;
import com.qianfeng.pojo.SysUserInfo;
import com.qianfeng.status.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/16 11:53
 */
@Service
public class MyUserDetailService implements UserDetailsService {
    private UserMapper userMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserInfo user = userMapper.findUserByName(username);
        if (user == null) {
            throw new FindDataException("用户名或密码错误", ResultCode.DATA_NOT_EXIST);
        }
//        GrantedAuthority 表示授予身份验证对象的权限。
        ArrayList<GrantedAuthority> arrayList = new ArrayList<>();

        return new BaseUser(username,user.getPassword(),arrayList,user.getUId());
    }
}
